package finbudget.client.ui.table.fields;

import java.util.Date;

import finbudget.client.ui.table.Transformator;

public class DateField extends StringField {
	private static final long serialVersionUID = 1355149909868363837L;

	/**
	 * Stores the default shift from the current date for the control.
	 */
	private Integer defaultDayShiftFromCurrentDate;

	/** Serialization requirements */
	protected DateField() {
		super();
	}

	public DateField(String id, String name, int row, int col) {
		this.type = Type.DATE;
		this.id = id;
		this.captionForm = name;
		this.captionGrid = name;
		this.row = row;
		this.column = col;
	}

	public DateField setDefaultDayShiftFromCurrentDate(Integer defaultDayShiftFromCurrentDate) {
		this.defaultDayShiftFromCurrentDate = defaultDayShiftFromCurrentDate;
		return this;
	}

	public Date getDefaultDate() {
		if (defaultDayShiftFromCurrentDate == null) {
			return null;
		}
		long time = defaultDayShiftFromCurrentDate.longValue();
		time *= 24L * 60L * 60L * 1000L; // shift in milliseconds.
		time += new Date().getTime();
		return new Date(time);
	}

	@Override
	public Object toRaw(Transformator transformator, Object value) {
		return transformator.toRaw(this, value);
	}

	@Override
	public String fromRaw(Transformator transformator, Object value) {
		return transformator.fromRaw(this, value);
	}

}
